/* Copyright (C) 2011 Riccardo Marson

   This file is part of OpenPool, an open-source game of pool.

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   RICCARDO MARSON BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
   AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  See Readme.rtf for further information.

*/

#include "CVector2.h"

#include <math.h>

//Constructors
CVector2::CVector2()
{
    mData[0] = 0.0f;
    mData[1] = 0.0f;
}

CVector2::CVector2(float x, float y)
{
    mData[0] = x;
    mData[1] = y;
}

CVector2::CVector2(float components[2])
{
    mData[0] = components[0];
    mData[1] = components[1];
}

CVector2::CVector2(const CVector2 &source)
{
    mData[0] = source.mData[0];
    mData[1] = source.mData[1];
}

CVector2::~CVector2()
{

}

//Opearators overloading
float& CVector2::operator() (const unsigned short i)
{
    //check if the requested index is out of bounds.
    if (i < 2)
        return mData[i];
    else
    {
        std::cout << "Error when calling CMatrix4x4 operator(), requested indexes are out of bounds." << std::endl;
        return mData[0];
    }
}

CVector2 CVector2::operator+ ( CVector2 vector )
{
    CVector2 out;

    out(0) = mData[0] + vector(0);
    out(1) = mData[1] + vector(1);

    return out;
}

CVector2 CVector2::operator+ (const float scalar)
{
    CVector2 out;

    out(0) = mData[0] + scalar;
    out(1) = mData[1] + scalar;

    return out;
}

CVector2 CVector2::operator- ( CVector2 vector )
{
    CVector2 out;

    out(0) = mData[0] - vector(0);
    out(1) = mData[1] - vector(1);

    return out;
}

CVector2 CVector2::operator- (const float scalar)
{
    CVector2 out;

    out(0) = mData[0] - scalar;
    out(1) = mData[1] - scalar;

    return out;
}

CVector2 CVector2::operator* ( CVector2 vector )
{
    CVector2 out;

    out(0) = mData[0]*vector(0);
    out(1) = mData[1]*vector(1);

    return out;
}

CVector2 CVector2::operator* ( const float scalar )
{
    CVector2 out;

    out(0) = mData[0]*scalar;
    out(1) = mData[1]*scalar;

    return out;
}

float CVector2::Dot( CVector2 vector )
{
   return ( mData[0] * vector(0) +  mData[1] * vector(1) );
}

CVector2 CVector2::Cross( CVector2 vector )
{
    CVector2 out;

    out(0) = mData[1]*vector(2) - mData[2]*vector(1);
    out(1) = mData[2]*vector(0) - mData[0]*vector(2);

    return out;
}

CVector2& CVector2::operator= ( CVector2 vector )
{
    //if the same instance as "this" is passed as argument, return this without performing
    //any operation
    if (this == &vector)
        return *this;

    mData[0] = vector(0);
    mData[1] = vector(1);

    return *this;
}

bool CVector2::operator== ( CVector2 vector )
{
    if ( (mData[0] == vector(0)) &&
         (mData[1] == vector(1)) )
        return true;
    else
        return false;
}

bool CVector2::operator!= (CVector2 vector)
{
    bool ret = false;

    if ( ( mData[0] == vector.x() ) && ( mData[1] == vector.y() ) )
        ret = true;

    return ret;
}

//other functions
float CVector2::GetLength()
{
    return sqrt( mData[0]*mData[0] + mData[1]*mData[1] );
}

CVector2 CVector2::Normalize()
{
	CVector2 out;

	float mag = GetLength();

	out.x() = mData[0] / mag;
	out.y() = mData[1] / mag;

	return out;
}

void CVector2::SetComponents(float x, float y)
{
    mData[0] = x;
    mData[1] = y;
}

float* CVector2::GetComponents()
{
    return mData;
}

float& CVector2::x()
{
    return mData[0];
}

float& CVector2::y()
{
    return mData[1];
}

CVector2 CVector2::Rotate(float angle, CVector2& vecIn)
{
    CVector2 out;

	float sAngle, cAngle;

	sAngle = sin(angle);
	cAngle = cos(angle);

    out.SetComponents( vecIn.x() * cAngle - vecIn.y() * sAngle,
                       vecIn.x() * sAngle + vecIn.y() * cAngle );

    return out;
}

std::ostream & operator << (std::ostream &out, CVector2 &vec)
{
    out << vec.mData[0] << " " << vec.mData[1];
    return out;
}


